package com.shuai.user.controller;

import com.shuai.common.constant.ErrorCode;
import com.shuai.common.exception.BusinessException;
import com.shuai.common.rps.BaseResponse;
import com.shuai.common.utils.ResultUtils;
import com.shuai.user.service.TencentCosService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {

    @Autowired
    private TencentCosService tencentCosService;

    @PostMapping("/upload")
    public BaseResponse<String> upload(@RequestPart("file") MultipartFile file) {
        try {
            return ResultUtils.success(tencentCosService.upload(file));
        } catch (IOException e) {
            log.error("文件:{} 上传失败", file.getOriginalFilename());
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "文件上传失败");
        }
    }
}
